import 'package:intl/intl.dart';
import 'package:largefriends/entity/json/json_parse_utils.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';

import 'lf_location_entity.dart';

class ChatNewMessageEntity {
  ChatNewMessageEntity(
      {this.created = 0,
      this.duration,
      this.historyId,
      this.message = "",
      this.messageType,
      this.profId,
      this.roomId,
      this.sender,
      this.url,
      this.userId,
      this.height,
      this.width,
      this.shouldShowTime = false,
      this.sendState,
      this.localId,
      this.replyInfo,
      this.groupId,
      this.groupType,
      this.clientId,
      this.companyInfo,
      this.xInfo,
      this.localImagePath //本地字段，判断发送的图片
      });

  factory ChatNewMessageEntity.fromJson(Map<dynamic, dynamic> jsonRes) =>
      ChatNewMessageEntity(
          created: jsonRes.asInt('created'),
          duration: jsonRes.asString('duration'),
          historyId: jsonRes.asString('historyId'),
          message: jsonRes.asString('message').trim(),
          messageType: jsonRes.asString('messageType'),
          profId: jsonRes.asString('profId'),
          roomId: jsonRes.asString('roomId'),
          sender: jsonRes.asBean('sender', (v) => Sender.fromJson(v)),
          replyInfo: jsonRes.asBean('replyInfo', (v) => ReplyInfo.fromJson(v)),
          url: jsonRes.asString('url'),
          userId: jsonRes.asString('userId'),
          height: jsonRes.asDouble('height'),
          width: jsonRes.asDouble('width'),
          shouldShowTime: jsonRes.asBool('shouldShowTime'),
          sendState: jsonRes.asInt('sendState'),
          groupId: jsonRes.asString('group_id'),
          groupType: jsonRes.asString('group_type'),
          clientId: jsonRes.asString('client_id'),
          xInfo: jsonRes
              .asBeans(['xInfo', 'x_info'], (json) => XInfo.fromJson(json)),
          companyInfo: jsonRes.asBean(
              "companyInfo", (json) => CompanionInfo.fromJson(json)),
          localImagePath: jsonRes.asString('localImagePath'),
          localId: jsonRes.asString('local_id'));

  int created;
  String? duration;
  String? historyId;
  String? message;
  String? messageType;
  String? profId;
  String? roomId;
  Sender? sender;
  String? url;
  String? userId;
  double? height;
  double? width;
  bool shouldShowTime; //本地字段，判断是否在聊天列表显示时间
  int? sendState = 0; //本地字段，判断消息是否发送成功 1:代表发送中,0代表发送成功,-1== 失败
  String? localId;
  ReplyInfo? replyInfo;
  String? localImagePath; //本地字段，判断发送的图片
  String? clientId;
  String? groupId;
  String? groupType;
  XInfo? xInfo;
  CompanionInfo? companyInfo;

  // @override
  // String toString() {
  //   return jsonEncode(this);
  // }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'created': created,
        'duration': duration,
        'historyId': historyId,
        'message': message,
        'messageType': messageType,
        'profId': profId,
        'roomId': roomId,
        'sender': sender,
        'url': url,
        'userId': userId,
        'height': height,
        'width': width,
        'shouldShowTime': shouldShowTime,
        'sendState': sendState,
        'replyInfo': replyInfo,
        'localImagePath': localImagePath,
        'localId': localId,
        'client_id': clientId,
        'group_type': groupType,
        'group_id': groupId,
        'xInfo': xInfo,
        'companyInfo': companyInfo
      };
  //
  // ///这里toUID 错误了,TODO
  // MessageEntity toMessageEntity(
  //     {String? localId, required String? otherUid, required String? selfUid}) {
  //   return MessageEntity(
  //       typeId: messageType,
  //       duration: duration == null ? 0 : int.parse(duration!),
  //       url: url,
  //       width: width,
  //       height: height,
  //       fromUid: sender?.profile?.userId,
  //       toUid: sender?.profile?.userId == selfUid ? otherUid : selfUid,
  //
  //       ///如果发送者是自己,那么接受者就是对方
  //       content: message,
  //       time: created,
  //       messageId: historyId,
  //       roomId: roomId,
  //       localId: localId,
  //       sendState: 0);
  // }

  bool isBlockedMe() {
    if (sender?.profile?.blockedMe == 1) {
      return true;
    }
    return false;
  }

  bool isHidden(LfUserEntity self) {
    if (sender?.profile?.hidden == 1) {
      return true;
    }
    if (sender?.profile?.hiddenGender == self.gender) {
      return true;
    }
    return false;
  }
}

class Sender {
  Sender({
    this.location,
    this.profile,
  });

  factory Sender.fromJson(Map<dynamic, dynamic> jsonRes) => Sender(
        location: jsonRes.asBean('location',
            (json) => LfLocationEntity.fromJson(jsonRes['location'])),
        profile: jsonRes.asBean(
            'profile', (json) => Profile.fromJson(jsonRes['profile'])),
      );

  LfLocationEntity? location;
  Profile? profile;

  // @override
  // String toString() {
  //   return jsonEncode(this);
  // }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'location': location,
        'profile': profile,
      };
}

class Profile {
  Profile({
    this.about,
    this.age,
    this.avatarUrl,
    this.gender = 0,
    this.member = 0,
    this.online = 0,
    this.thumbUrl,
    this.userId,
    this.username,
    this.verified = 0,
    this.status = 0,
    this.hidden = 0,
    this.blockedMe = 0,
    this.hiddenGender = 0,
  });

  factory Profile.fromJson(Map<String, dynamic> jsonRes) => Profile(
        about: jsonRes.asString('about'),
        age: jsonRes.asString('age'),
        avatarUrl: jsonRes.asStrings(['avatarUrl', 'avatar']),
        gender: jsonRes.asInt('gender'),
        member: jsonRes.asInt('member'),
        online: jsonRes.asInt('online'),
        thumbUrl: jsonRes.asStrings(['thumbUrl', 'avatarThumb']),
        userId: jsonRes.asString('userId'),
        username: jsonRes.asString('username'),
        hidden: jsonRes.asInt('hidden'),
        blockedMe: jsonRes.asInt('blockedMe'),
        hiddenGender: jsonRes.asInt('hiddenGender'),
      );

  String? about;
  String? age;
  String? avatarUrl;
  int gender;
  int member;
  int online;
  String? thumbUrl;
  String? userId;
  String? username;
  int blockedMe;
  int hidden;
  int hiddenGender;

  //profile status, 0: pending, 1: approved, 2: refused, 3: blocked, 4: removed, 5: on hold
  int status = 0;
  int verified;

  // @override
  // String toString() {
  //   return jsonEncode(this);
  // }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'about': about,
        'age': age,
        'avatarUrl': avatarUrl,
        'gender': gender,
        'member': member,
        'online': online,
        'thumbUrl': thumbUrl,
        'userId': userId,
        'username': username,
        'blockedMe': blockedMe,
        'hidden': hidden,
        'hiddenGender': hiddenGender,
      };
}

class ReplyInfo {
  ReplyInfo({
    this.historyId,
    this.content,
    this.toUsername,
  });
  String? historyId;
  String? content;
  String? toUsername;

  factory ReplyInfo.fromJson(Map<dynamic, dynamic> jsonRes) => ReplyInfo(
        historyId: jsonRes.asString('history_id'),
        content: jsonRes.asString('content'),
        toUsername: jsonRes.asString('to_username'),
      );

  Map<String, dynamic> toJson() => <String, dynamic>{
        'history_id': historyId,
        'content': content,
        'to_username': toUsername,
      };
}

class CompanionInfo {
  CompanionInfo(
      {this.title,
      this.active,
      this.companionId,
      this.departureTime,
      this.expectedTime,
      this.destination});
  String? companionId;
  int? expectedTime;
  int? departureTime;
  int? active;
  String? title;
  LfLocationEntity? destination;

  factory CompanionInfo.fromJson(Map<dynamic, dynamic> jsonRes) =>
      CompanionInfo(
          title: jsonRes.asString('title'),
          expectedTime: jsonRes.asInt('expectedTime'),
          departureTime: jsonRes.asInt('departureTime'),
          companionId: jsonRes.asString('companionId'),
          active: jsonRes.asInt('active'),
          destination: jsonRes.asBean(
              "destination", (json) => LfLocationEntity.fromJson(json)));

  Map<String, dynamic> toJson() => <String, dynamic>{
        'title': title,
        'active': active,
        'companionId': companionId,
        'departureTime': departureTime,
        'expectedTime': expectedTime,
        'destination': destination,
      };

  String getStartDate() {
    DateTime dateTime =
        DateTime.fromMillisecondsSinceEpoch(departureTime ?? 0 * 1000);
    return DateFormat('MMM d, y').format(dateTime);
  }

  String getEndDate() {
    DateTime dateTime =
        DateTime.fromMillisecondsSinceEpoch(expectedTime ?? 0 * 1000);
    return DateFormat('MMM d, y').format(dateTime);
  }
}

class XInfo {
  XInfo({
    this.url,
  });

  String? url;

  factory XInfo.fromJson(Map<dynamic, dynamic> jsonRes) => XInfo(
        url: jsonRes.asString('url'),
      );

  Map<String, dynamic> toJson() => <String, dynamic>{
        'url': url,
      };
}
